package day13.CollectionTask;

import java.lang.reflect.Array;
import java.util.*;

public class SendMessage2 {
    public static void main(String[] args) {

        Map<String, Map<String, List<String>>> map = new HashMap<>();
        String[] str1 = {"136", "137", "181", "180", "150"};
        String[] str_136 = {"0371", "0766", "7335", "7362", "6385", "0769", "7494", "3381"};
        String[] str_137 = {"3383", "3319", "0088", "8361", "3315", "8168", "0386", "8151"};
        String[] str_181 = {"3788", "3789", "3782", "3787", "0394", "3567", "2234", "0382"};
        String[] str_180 = {"3951", "0169", "3991", "3955", "3928", "3788", "0387", "3997", "3923"};
        String[] str_150 = {"3981", "3719", "0371", "3816", "0389", "3681", "9326", "3837", "3802"};
        String[][] str = {str_136, str_137, str_181, str_180, str_150};
        // 添加手机号码段到Map集合中
        for (int i = 0; i < str1.length; i++) {
            Map<String, List<String>> map1 = new HashMap<>();
            for (int j = 0; j < str[i].length; j++) {
                List<String> list = new ArrayList<>();
                for (int k = 1000; k <= 9999; k++) {
                    String phoneNumber = str1[i] + str[i][j] + String.format("%04d", k);
                    list.add(phoneNumber);
                }
                map1.put(str[i][j], list);
            }
            map.put(str1[i], map1);
        }

        // 从控制台输入要发送信息的号码和短信内容
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要发送信息的号码（多个号码用英文逗号分隔）：");
        String phoneNumberInput = scanner.nextLine();
        System.out.print("请输入短信内容：");
        String message = scanner.nextLine();
        // 拆分输入的号码
        String[] phoneNumbersToSend = phoneNumberInput.split(",");
        // 发送短信
        for (
                String phoneNumber : phoneNumbersToSend) {
            if (phoneNumber.isEmpty()) {
                System.out.println("号码不能为空！");
                continue;
            }
            if (contains(map, phoneNumber)) {
                System.out.println("红名单用户：" + phoneNumber);
                long startTime = System.currentTimeMillis();
                // 模拟校验耗时
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long endTime = System.currentTimeMillis();
                System.out.println("校验耗时：" + (endTime - startTime) + "ms");
            }
            // 模拟短信发送成功概率为98%
            if (Math.random() < 0.98) {
                System.out.println("发送成功：" + phoneNumber + "，内容：" + message);
            }
        }
    }
    public static boolean contains(Map<String, Map<String, List<String>>> map, String str) {
        String headPhone = str.substring(0, 3);
        String middlePhone = str.substring(3, 7);
        String lastPhone = str.substring(7);
        boolean flag = false;
        Map<String, List<String>> map1 = map.get(headPhone);
        if (map1 != null && !map1.isEmpty()) {
            List<String> list = map1.get(middlePhone);
            if (list != null && !list.isEmpty()) {
                if (list.contains(str)) {
                    flag = true;
                    return flag;
                }
            }
        }
        return flag;
    }
}
